package org.xncj.Impl;

import org.dto.NumberDto;
import org.dto.OrderDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.utils.DateUtils;
import org.vo.OrderVo;
import org.xncj.Order;
import org.xncj.ReportService;
import org.xncj.dao.ReportDao;


import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Resource
    ReportDao reportDao;

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception{
        Map<String, Object> map = new HashMap<>();

        // 当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate = sdf.format(new Date());
        map.put("reportDate", reportDate);

        // 今日新增会员数
        Integer todayNewMember = reportDao.findTodayMember(reportDate);///////////////////////
        map.put("todayNewMember", todayNewMember);

//
        // 总会员数
        Integer totalMember = reportDao.findMember();/////////////////////////////////////////
        map.put("totalMember", totalMember);

        // 本周新增会员数
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        NumberDto WeekDto = new NumberDto();

        WeekDto.setStartTime(thisWeekMonday);
        String endTimeStr = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
        WeekDto.setEndTime(endTimeStr);
        Integer thisWeekNewMember = reportDao.findThisWeekNewCount(WeekDto);//////////////////////
        map.put("thisWeekNewMember", thisWeekNewMember);

//      // 本月新增会员数
        String thisMonthFirst = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        NumberDto monberDto = new NumberDto();
        monberDto.setStartTime(thisMonthFirst);
        int endTime = Integer.parseInt(thisMonthFirst.substring(thisMonthFirst.lastIndexOf("-") + 1));
        endTime = endTime + 29;
        String endTimeMonth = thisMonthFirst.substring(0, thisMonthFirst.lastIndexOf("-")+1)+endTime;
        monberDto.setEndTime(endTimeMonth);
        Integer thisMonthNewMember = reportDao.findThisMonthCount(monberDto);///////////////
        map.put("thisMonthNewMember", thisMonthNewMember);




//      // 今日到诊数
        // 创建 OrderVo 对象并设置属性
        OrderVo orderVo = new OrderVo();
        orderVo.setOrderDate(reportDate); // reportDate 是一个 Date 类型的变量，代表查询的日期
        orderVo.setOrderStatus(Order.ORDERSTATUS_YES); // 假设 Order.ORDERSTATUS_YES 是一个表示订单已完成状态的常量

// 调用 reportDao 的方法查询今日订单数量
        Integer todayVisitsNumber = reportDao.findTodayVisitsNumber(orderVo);
        map.put("todayVisitsNumber", todayVisitsNumber); // 将查询结果放入 map 中，供后续使用

        //本月预约数
        OrderDto orderDto = new OrderDto();
        orderDto.setOrderStatus(thisMonthFirst);
        orderDto.setEndTime(endTimeStr);
        orderDto.setOrderStatus(Order.ORDERSTATUS_YES);
        Integer thisMonthOrderNumber = reportDao.findThisWeekVisitsTaday(orderDto);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);

//      // 今日预约数
        Integer todayOrderNumber = reportDao.findTodayOrderCountByDate(reportDate);
        map.put("todayOrderNumber", todayOrderNumber);


        // 本周预约数
        orderDto.setStartTime(thisMonthFirst);
        orderDto.setEndTime(endTimeStr);
        orderDto.setOrderStatus(Order.ORDERSTATUS_YES);
        Integer thisWeekOrderNumber = reportDao.findTodayOrderCountByTaday(orderDto);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);

        // 本周到诊数
        orderDto.setStartTime(thisMonthFirst);
        orderDto.setEndTime(endTimeStr);
        orderDto.setOrderStatus(Order.ORDERSTATUS_YES);
        Integer thisWeekVisitsNumber = reportDao.findThisWeekVisitsCount(orderDto);///////////////////
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);

        // 本月到诊数
        OrderDto orderMonthDto = new OrderDto();
        orderMonthDto.setStartTime(thisMonthFirst);
        orderMonthDto.setEndTime(endTimeMonth);
        orderMonthDto.setOrderStatus(Order.ORDERSTATUS_YES);
        Integer thisMonthVisitsNumber = reportDao.findThisMonthVisitsCount(orderMonthDto);/////////
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        //*-
//
        List<Map> hotSetmeal = reportDao.findHotSetmel();/////////////////////////////////////////
        map.put("hotSetmeal", hotSetmeal);

        return map;
    }

}